<?php

namespace app\store\model\dealer;

use app\common\model\dealer\Order as OrderModel;
use app\common\service\Order as OrderService;

/**
 * 分销商订单模型
 * Class Apply
 * @package app\store\model\dealer
 */
class Order extends OrderModel
{
    public function getList($user_id = null, $start_time = '', $end_time = '', $search = '', $pay_status = '')
    {
        // 检索查询条件
        if ($user_id > 1) {
            $this->where('first_user_id|second_user_id|third_user_id', '=', $user_id);
        }
    
        // 添加医生名称、推荐人真实姓名和推荐人昵称的模糊搜索
        if (!empty($search)) {
            $this->where(function ($query) use ($search) {
                $query->where('og.goods_name', 'like', "%{$search}%") // 医生名称
                    ->whereOr('du.real_name', 'like', "%{$search}%") // 推荐人真实姓名
                    ->whereOr('u_recommender.nickName', 'like', "%{$search}%"); // 推荐人昵称
            });
        }
    
        // 时间区间条件（查询挂号时间）
        if (!empty($start_time) || !empty($end_time)) {
            $startTimestamp = !empty($start_time) ? strtotime($start_time . ' 00:00:00') : null;
            $endTimestamp = !empty($end_time) ? strtotime($end_time . ' 23:59:59') : null;
    
            if ($startTimestamp && $endTimestamp) {
                $this->whereBetween('og.create_time', [$startTimestamp, $endTimestamp]);
            } elseif ($startTimestamp) {
                $this->where('og.create_time', '>=', $startTimestamp);
            } elseif ($endTimestamp) {
                $this->where('og.create_time', '<=', $endTimestamp);
            }
        }
    
        // 支付状态条件（订单表）
        if (!empty($pay_status)) {
            $this->where('o.pay_status', '=', $pay_status); // 10：未付款，20：已付款
        }
    
        // 获取分销商订单列表，保留现有 with 关联
        $data = $this->with([
            'dealer_first.user',
            'dealer_second.user',
            'dealer_third.user'
        ])
            ->alias('dealer_order')
            ->join('order_goods og', 'dealer_order.order_id = og.order_id', 'LEFT') // 关联 order_goods 表
            ->join('user_jiuzhen uj', 'og.jiuzhen_id = uj.jiuzhen_id', 'LEFT') // 关联 user_jiuzhen 表
            ->join('order o', 'dealer_order.order_id = o.order_id', 'LEFT') // 关联 order 表
            ->join('user u_recommender', 'dealer_order.first_user_id = u_recommender.user_id', 'LEFT') // 关联推荐人用户表
            ->join('dealer_user du', 'dealer_order.first_user_id = du.user_id', 'LEFT') // 关联 dealer_user 表获取真实姓名
            ->field([
                'dealer_order.*',
                'og.create_time as order_goods_create_time', // 挂号时间
                'og.goods_name as doctor_name', // 医生名称
                'uj.name as jiuzhen_name',
                'uj.phone as jiuzhen_phone',
                'uj.create_time as jiuzhen_create_time',
                'o.pay_status as order_pay_status', // 订单付款状态
                'u_recommender.nickName as recommender_nickname', // 推荐人昵称
                'du.real_name as recommender_real_name', // 推荐人真实姓名
            ])
            ->order(['dealer_order.create_time' => 'desc'])
            ->paginate(10, false, [
                'query' => \request()->request()
            ]);
    
        if ($data->isEmpty()) {
            return $data;
        }
    
        // 保持现有的订单主信息处理逻辑
        $with = ['goods' => ['image', 'refund'], 'address', 'user'];
        return OrderService::getOrderList($data, 'order_master', $with);
    }

}